package org.vidyakiran.app.client.controller;

import java.util.ArrayList;
import java.util.List;

import org.vidyakiran.app.client.view.LoginPopup;
import org.vidyakiran.app.client.view.MainView;
import org.vidyakiran.app.client.view.PostNewActivityPopUp;
import org.vidyakiran.app.client.view.ResultsView;
import org.vidyakiran.app.client.vos.ActivityInfo;
import org.vidyakiran.app.client.vos.SchoolInfo;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TabPanel;

public class MainController {
	
	private MainView mainView;
	private List<SchoolInfo> schoolList;
	private Label noResults;
	private Button postNew;
	private boolean loggedIn;
	private boolean fromPostNewActivityButton;
	private LoginPopup loginPopup;
	private PostNewActivityPopUp postNewActivityPopUp;

	public MainController() {
		mainView = new MainView();
		noResults = new Label("Sorry no results were found to match your criteria.");
		postNew = new Button("Post new activity");
		addlisteneres();
		initialiseData();
	}

	public TabPanel getMainView() {
			return mainView.createMainView();
	}
	
	public void addlisteneres(){
		//addTabListener();
		//addButtonListener();
	}
	
	private void addTabListener(){
		mainView.getTabView().getHomeButton().addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {

				mainView.getBodyPanel().clear();
				mainView.getBodyPanel().add(mainView.getHomeView().createHomePanel());
				
			}	
		});
		
		mainView.getTabView().getVolunteerButton().addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {

				mainView.getBodyPanel().clear();
				mainView.getBodyPanel().add(mainView.getVolunteerView().createVolunteerView());
				
			}	
		});
		
		mainView.getTabView().getSchoolsButton().addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {

				mainView.getBodyPanel().clear();
				//mainView.getBodyPanel().add(mainView.getSchoolView());
				
			}	
		});
	}
	
	
	private void addButtonListener(){
		//mainView.getVolunteerView().getSearchButton().setText("HIIII");
		mainView.getVolunteerView().getSearchButton().addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				String query = mainView.getVolunteerView().getQueryTextBox().getText();
				mainView.getVolunteerView().getResultsPanel().clear();
				List<SchoolInfo> results = new ArrayList<SchoolInfo>();
				if(query!=null && !query.equals("")){
					for (SchoolInfo school : schoolList) {
						List<ActivityInfo> activiryList = school.getActivities();
						for (ActivityInfo activityInfo : activiryList) {
							if(activityInfo.getActivityName().equals(query)){
								System.out.println("activity is" +activityInfo.getActivityName());
								results.add(school);
								mainView.getVolunteerView().getResultsPanel().add(new ResultsView(school).createResultsPanel());
							}
						}
					}
				}
				
				if(results.isEmpty()){
					
					mainView.getVolunteerView().getResultsPanel().add(noResults);
					mainView.getVolunteerView().getResultsPanel().add(postNew);
					
					
				}
					
					
				
			}
			
		});
		
		postNew.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				
				if(!loggedIn){
					fromPostNewActivityButton = true;
					initLoginPopup();
				}else{
					initPostNewActivityPopup();
				}
			}
			
		});
		
	}
	
	private void initLoginPopup(){
		
		loginPopup = new LoginPopup();
		loginPopup.center();
		loginPopup.getRegister().addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				// TODO Show registeration window
				
			}
			
		});
		
		loginPopup.getLoginButton().addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				loggedIn = true;
		
				
			if(fromPostNewActivityButton){
					initPostNewActivityPopup();
				loginPopup.hide(true);
					
					
				}else{
					//TODO show volunteer for activities view.

				}
				
				
			}
			
		});
		
		loginPopup.getCancelButton().addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				loggedIn = false;
				loginPopup.hide(true);
				
			}
			
		});
		
		
	}
	
	
	private void initPostNewActivityPopup(){
		//TODO activity view					
		postNewActivityPopUp = new PostNewActivityPopUp();
		postNewActivityPopUp.getOkButton().addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				postNewActivityPopUp.hide(true);
				
			}
			
		});
		
		postNewActivityPopUp.getCancelButton().addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				postNewActivityPopUp.hide(true);
				
			}
			
		});
		
		postNewActivityPopUp.center();
	}
	private void initialiseData(){
		ActivityInfo activityInfo1 = new ActivityInfo();
		activityInfo1.setActivityName("Guitar");
		activityInfo1.setStartTime("3pm");
		activityInfo1.setLastTime("3:30pm");
		
		ActivityInfo activityInfo2 = new ActivityInfo();
		activityInfo2.setActivityName("French");
		activityInfo2.setStartTime("7pm");
		activityInfo2.setLastTime("7:30pm");
		
		ActivityInfo activityInfo3 = new ActivityInfo();
		activityInfo3.setActivityName("Sewing");
		activityInfo3.setStartTime("8pm");
		activityInfo3.setLastTime("8:30pm");
		
		SchoolInfo school1 = new SchoolInfo();
		school1.setName("School 1");
		school1.setAddress("LBS marg Bhandup,Mumbai");
		school1.setLocation("Bhandup");
		school1.getActivities().add(activityInfo1);
		school1.getActivities().add(activityInfo2);
		school1.getActivities().add(activityInfo3);
		
		SchoolInfo school2 = new SchoolInfo();
		school2.setName("School 2");
		school2.setAddress("Yogi Nagar,Mumbai");
		school2.setLocation("Borivali");
		school2.getActivities().add(activityInfo2);
		school2.getActivities().add(activityInfo3);
		
		
		SchoolInfo school3 = new SchoolInfo();
		school3.setName("School 3");
		school3.setAddress("Seven Bungalows,Mumbai");
		school3.setLocation("Andheri");
		school3.getActivities().add(activityInfo1);
		school3.getActivities().add(activityInfo3);
		
		schoolList = new ArrayList<SchoolInfo>();
		schoolList.add(school1);
		schoolList.add(school2);
		schoolList.add(school3);
		
	}
	
	

}
